package edu.ynu.se.xiecheng.achitectureclass.common.service;

import edu.ynu.se.xiecheng.achitectureclass.common.dao.CameraRecodeDAO;
import edu.ynu.se.xiecheng.achitectureclass.common.dao.CommonRecodeDAO;
import edu.ynu.se.xiecheng.achitectureclass.common.dao.DeviceDAO;
import edu.ynu.se.xiecheng.achitectureclass.common.dao.GreenhouseDAO;
import edu.ynu.se.xiecheng.achitectureclass.common.dto.CameraDeviceValueDTO;
import edu.ynu.se.xiecheng.achitectureclass.common.dto.CommonDeviceValueDTO;
import edu.ynu.se.xiecheng.achitectureclass.common.entity.CameraRecode;
import edu.ynu.se.xiecheng.achitectureclass.common.entity.CommonRecode;
import edu.ynu.se.xiecheng.achitectureclass.common.entity.Device;
import edu.ynu.se.xiecheng.achitectureclass.common.utils.CameraDeviceTokenUtil;
import edu.ynu.se.xiecheng.achitectureclass.common.utils.CameraDeviceValueUtil;
import edu.ynu.se.xiecheng.achitectureclass.common.utils.CommonDeviceTokenUtil;
import edu.ynu.se.xiecheng.achitectureclass.common.utils.CommonDeviceValueUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Service
public class CameraRecodeService {

    @Autowired
    private CameraRecodeDAO cameraRecodeDAO;


    public HashMap<String, String> ObtainDataFromServer(
            String accessToken,
            String deviceSerial,
            String channelNo,
            String quality
    ) throws IOException {
        HashMap<String, CameraRecode> cameraRecodeHashMap = new HashMap<>();

        CameraDeviceValueDTO cameraDeviceValueDTO = CameraDeviceValueUtil.getCaptureData(accessToken, deviceSerial, channelNo, quality);
        HashMap<String, String> result = new HashMap<>();
        if (cameraDeviceValueDTO.getCode().equals("200")) {
            CameraRecode cameraRecode = new CameraRecode();
            cameraRecode.setIsDeleted(0);
            cameraRecode.setQuality(Integer.parseInt(quality));
            cameraRecode.setSeriesId(deviceSerial);
            cameraRecode.setChannelNo(Integer.parseInt(channelNo));
            cameraRecode.setImgUrl(cameraDeviceValueDTO.getData().getPicUrl());
            result.put("quality", quality);
            result.put("imgUrl", cameraDeviceValueDTO.getData().getPicUrl());
            cameraRecodeDAO.save(cameraRecode);
        }
        else{
            result.put("quality", quality);
            result.put("imgUrl", "127.0.0.1:8085/img");
        }
        return result;
    }

    public String getCameraToken() throws IOException {
        return CameraDeviceTokenUtil.getToken("1ca7f0ab24ba41b291346347ec30140e").getData().getAccessToken();
    }
}
